package cn.edu.hqu.wel.shiyan1.model;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Data
public class Comment {
    private Long id;
    private Movie movie;
    private String content;
    private Integer rating;
    private Integer likes = 0;

    // 添加构造函数
    public Comment(Long id, Movie movie, String content, Integer rating, Integer likes) {
        this.id = id;
        this.movie = movie;
        this.content = content;
        this.rating = rating;
        this.likes = likes;
    }

    public static void addComment(Comment comment) {
        Optional<Movie> movieOptional = Movie.getMovieById(comment.getMovie().getId());
        if (movieOptional.isPresent()) {
            comment.setId((long) (movieOptional.get().getComments().size() + 1));
            movieOptional.get().addComment(comment);
        }
    }

    public static List<Comment> getCommentsByMovieId(Long movieId) {
        Optional<Movie> movieOptional = Movie.getMovieById(movieId);
        return movieOptional.isPresent() ? movieOptional.get().getComments() : new ArrayList<>();
    }

    public static Optional<Comment> getCommentById(Long id) {
        for (Movie movie : Movie.getAllMovies()) {
            for (Comment comment : movie.getComments()) {
                if (comment.getId().equals(id)) {
                    return Optional.of(comment);
                }
            }
        }
        return Optional.empty();
    }

    public static void likeComment(Long commentId) {
        Optional<Comment> commentOptional = getCommentById(commentId);
        if (commentOptional.isPresent()) {
            commentOptional.get().setLikes(commentOptional.get().getLikes() + 1);
        }
    }
}